"""商品数据模型
"""
from sqlalchemy import Column, Integer, String, Text, DECIMAL, Enum, DateTime, JSON, ForeignKey, UniqueConstraint, Boolean
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from database import Base

class Product(Base):
    __tablename__ = "products"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(255), nullable=False, index=True)
    description = Column(Text, nullable=True)
    price = Column(DECIMAL(10, 2), nullable=False, index=True)
    category_id = Column(Integer, ForeignKey("categories.id"), nullable=False, index=True)
    # campus_id = Column(Integer, ForeignKey("campuses.id"), nullable=False, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
    images = Column(JSON, nullable=True)
    status = Column(Enum('active', 'sold', 'inactive'), default='active', index=True)
    view_count = Column(Integer, default=0)
    is_featured = Column(Boolean, default=False, index=True)
    condition_rating = Column(Enum('new', 'like_new', 'good', 'fair', 'poor'), nullable=True)
    tags = Column(JSON, nullable=True)
    location = Column(String(255), nullable=True)
    contact_method = Column(Enum('message', 'phone', 'wechat'), default='message')
    created_at = Column(DateTime, default=func.now(), index=True)
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
    
    # 关系
    category = relationship("Category", back_populates="products")
    # campus = relationship("Campus", back_populates="products")  # 需要定义Campus模型
    user = relationship("User", back_populates="products")
    product_images = relationship("ProductImage", back_populates="product")
    trades = relationship("Trade", back_populates="product")
    views = relationship("ProductView", back_populates="product")
    favorites = relationship("Favorite", back_populates="product")

class ProductImage(Base):
    __tablename__ = "product_images"
    
    id = Column(Integer, primary_key=True, index=True)
    product_id = Column(Integer, ForeignKey("products.id"), nullable=False, index=True)
    image_url = Column(String(255), nullable=False)
    sort_order = Column(Integer, default=0)
    is_primary = Column(Boolean, default=False)
    created_at = Column(DateTime, default=func.now())
    
    # 关系
    product = relationship("Product", back_populates="product_images")

class ProductView(Base):
    __tablename__ = "product_views"
    
    id = Column(Integer, primary_key=True, index=True)
    product_id = Column(Integer, ForeignKey("products.id"), nullable=False, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    ip_address = Column(String(45), nullable=True)
    viewed_at = Column(DateTime, default=func.now(), index=True)
    
    # 关系
    product = relationship("Product", back_populates="views")
    user = relationship("User", back_populates="product_views")

class Favorite(Base):
    __tablename__ = "favorites"
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
    product_id = Column(Integer, ForeignKey("products.id"), nullable=False, index=True)
    created_at = Column(DateTime, default=func.now(), index=True)
    
    # 关系
    user = relationship("User", back_populates="favorites")
    product = relationship("Product", back_populates="favorites")

    __table_args__ = (
        # 确保用户不能重复收藏同一商品
        UniqueConstraint('user_id', 'product_id', name='uq_user_product'),
    )